home *** CD-ROM | disk | FTP | other *** search
- 1 '*** Contour Map example: CONTOUR1.BAS
- 2 CLS: PRINT "CONREC Example. Coutour the function ":PRINT
- 3 PRINT "f(x,y)=sin((x^2+y^2)^0.5) + ((x-c)^2+y^2)^-0.5 ":PRINT
- 4 PRINT "letting x and y range from -2pi to +2pi.":PRINT
- 5 PRINT "Building the data set now. Please wait..."
- 6 OPTION BASE 0 'lower bound of zero for all array indices, instead of 1
- 7 PI#=3.141592654#
- 8 TRUE=-1: FALSE=0
- 9 ILENGTH=319: JLENGTH=199 'dimensions of output plot axes (CGA full screen)
- 10 IMIN=0: JMIN=199 'coords of left bottom corner of screen
- 11 IUB=30: JUB=30: NC=10 'number of grid intervals and contour levels
- 12 DIM D(IUB,JUB),X(IUB),Y(JUB) 'data array
- 13 DIM Z(NC-1)
- 14 ' define function and coords
- 15 FOR I=0 TO IUB 'check all x-grid levels
- 16 IX=2*PI#*(2*I-IUB)/IUB 'ix ranges from -2pi to +2pi
- 17 FOR J=0 TO JUB 'check all y-grid levels
- 18 JY=2*PI#*(2*J-JUB)/JUB 'jy ranges from -2pi to +2pi
- 19 R=SQR(IX^2+JY^2)
- 20 D(I,J)=SIN(R)+0.5/SQR((IX+3.05)^2 + IY^2) 'evaluate user supplied function,fill array
- 21 NEXT J
- 22 X(I)=I*ILENGTH/IUB+IMIN 'scale x(i) to span plot area
- 23 NEXT I
- 24 FOR J=0 TO JUB
- 25 Y(J)=JMIN-J*JLENGTH/JUB 'scale y(i) to span plot area
- 26 NEXT J
- 27 FOR I=0 TO NC-1
- 28 Z(I)=(I-5)/5 'contour levels at -1,-0.8,-0.6, ... ,1
- 29 NEXT I
- 30 '
- 31 CLS: SCREEN 1,0 'set CGA screen 320 x 200
- 32 '
- 33 LINE(IMIN,JMIN-JLENGTH)-(IMIN+ILENGTH,JMIN),,B 'use a box for axes
- 38 GOSUB 100 'Do conrec subroutine
- 40 IF NOT(PRMERR) THEN PRINT:PRINT:PRINT MSG$;
- 45 PRINT CHR$(7); 'alert user that program has finished output
- 50 WHILE LEN(INKEY$)=0: WEND 'press any key to finish
- 60 END
- 70 '
- 80 '
- 99 '
- 100 '**************************** CONREC.BAS *******************************
- 101 'Source: Translated from original program by Paul Bourke,
- 102 ' "A Contouring Subroutine", p.145, BYTE Magazine (June 1987)
- 103 'Input:
- 104 ' d(0:iub, 0:jub) matrix for data surface
- 105 ' iub,jub index bounds of data array
- 106 ' x(0:iub) array for column coords
- 108 ' y(0:jub) array for row coords
- 109 ' nc number of contour levels
- 110 ' z(0:nc-1) contour levels to use, in increasing order
- 111 ' VECOUT external subroutine to plot contour lines
- 112 '
- 113 '
- 180 FALSE=0: TRUE=-1
- 200 DIM H(4) 'releative heights of box above contour
- 201 DIM ISH(4) 'sign of h()
- 202 DIM XH(4) 'x coords of box
- 203 DIM YH(4) 'y coords of box
- 204 DIM IM(3) 'mapping from vertex numbers to x offsets
- 205 '
- 206 IM(0)=0: IM(1)=1: IM(2)=1: IM(3)=0
- 207 DIM jm(3) 'mapping from vertex numbers to y offsets
- 208 JM(0)=0: JM(1)=0: JM(2)=1: JM(3)=1
- 209 DIM CASTAB(2,2,2) 'case switch table
- 210 '
- 220 DATA 0,0,8,0,2,5,7,6,9,0,3,4,1,3,1,4,3,0,9,6,7,5,2,0,8,0,0
- 221 '
- 230 FOR K=O TO 2: FOR J=0 TO 2: FOR I=0 TO 2
- 240 READ CASTAB(K,J,I)
- 250 NEXT I: NEXT J: NEXT K
- 251 '
- 253 'Validate input params (PRMERR is parameter error flag)
- 254 '
- 260 PRMERR=FALSE
- 270 IF (IUB<=0)OR(JUB<=0) THEN PRMERR=TRUE
- 280 IF NC<=0 THEN PMERR=TRUE
- 290 FOR K=1 TO NC-1
- 292 IF (Z(K)<=Z(K-1)) THEN PRMERR=TRUE
- 294 NEXT K
- 295 IF PRMERR THEN MSG$="Error in input parameters":RETURN
- 297 '
- 298 'Scan array, top down and left to right
- 299 '
- 300 FOR J=JUB-1 TO 0 STEP -1 'downto
- 310 FOR I=0 TO IUB-1
- 311 ' find lowest vertex of the four
- 320 IF (D(I,J)<D(I,J+1)) THEN DMIN=D(I,J) ELSE DMIN=D(I,J+1)
- 321 IF (D(I+1,J)<DMIN) THEN DMIN=D(I+1,J)
- 322 IF (D(I+1,J+1)<DMIN) THEN DMIN=D(I+1,J+1)
- 324 ' find the highest vertex
- 325 IF (D(I,J)>D(I,J+1)) THEN DMAX=D(I,J) ELSE DMAX=D(I,J+1)
- 326 IF (D(I+1,J)>DMAX) THEN DMAX=D(I+1,J)
- 327 IF (D(I+1,J+1)>DMAX) THEN DMAX=D(I+1,J+1)
- 328 '
- 329 IF (DMAX<Z(0) OR DMIN>Z(NC-1)) THEN GOTO 9000
- 330 '
- 331 'Draw each contour withing this box
- 332 FOR K=0 TO NC-1
- 340 IF (Z(K)<DMIN) OR (Z(K)>DMAX) THEN GOTO 8000
- 350 FOR M=4 TO 0 STEP -1
- 360 IF M>0 THEN GOSUB 11000
- 370 IF M=0 THEN GOSUB 12000
- 380 IF H(M)>0 THEN ISH(M)=2 :ELSE IF (H(M)<0) THEN ISH(M)=0 :ELSE ISH(M)=1
- 390 NEXT M
- 391 '
- 400 ' Scan each triangle within box
- 401 '
- 410 FOR M=1 TO 4
- 420 M1=M: M2=0: M3=M+1: IF (M3=5) THEN M3=1
- 425 '
- 430 CASE = INT(CASTAB( ISH(M1),ISH(M2),ISH(M3) ))
- 440 IF CASE=0 THEN GOTO 7000
- 450 ON CASE GOTO 1100,1200,1300,1400,1500,1600,1700,1800,1900
- 460 '
- 1100 'line between vertices m1 and m2 (case 1)
- 1110 X1=XH(M1): Y1=YH(M1): X2=XH(M2): Y2=YH(M2)
- 1120 GOTO 6000
- 1130 '
- 1200 'line between vertices m2 and m3 (case 2)
- 1210 X1=XH(M2): Y1=YH(M2): X2=XH(M3): Y2=YH(M3)
- 1220 GOTO 6000
- 1230 '
- 1300 'line between vertices m3 and m1 (case 3)
- 1310 X1=XH(M3): Y1=YH(M3): X2=XH(M1): Y2=YH(M1)
- 1320 GOTO 6000
- 1330 '
- 1400 'line between vertex m1 and side m2-m3 (case 4)
- 1410 X1=XH(M1): Y1=YH(M1)
- 1420 X2=(H(M3)*XH(M2)-H(M2)*XH(M3))/(H(M3)-H(M2))
- 1430 Y2=(H(M3)*YH(M2)-H(M2)*YH(M3))/(H(M3)-H(M2))
- 1440 GOTO 6000
- 1450 '
- 1500 'line between vertex m2 and side m3-m1 (case 5)
- 1510 X1=XH(M2): Y1=YH(M2)
- 1520 X2=(H(M1)*XH(M3)-H(M3)*XH(M1))/(H(M1)-H(M3))
- 1530 Y2=(H(M1)*YH(M3)-H(M3)*YH(M1))/(H(M1)-H(M3))
- 1540 GOTO 6000
- 1550 '
- 1600 'line between vertex m3 and side m1-m2 (case 6)
- 1610 X1=XH(M3): Y1=YH(M3)
- 1620 X2=(H(M2)*XH(M1)-H(M1)*XH(M2))/(H(M2)-H(M1))
- 1630 Y2=(H(M2)*YH(M1)-H(M1)*YH(M2))/(H(M2)-H(M1))
- 1640 GOTO 6000
- 1650 '
- 1700 'line between sides m1-m2 and m2-m3 (case 7)
- 1710 X1=(H(M2)*XH(M1)-H(M1)*XH(M2))/(H(M2)-H(M1))
- 1720 Y1=(H(M2)*YH(M1)-H(M1)*YH(M2))/(H(M2)-H(M1))
- 1730 X2=(H(M3)*XH(M2)-H(M2)*XH(M3))/(H(M3)-H(M2))
- 1740 Y2=(H(M3)*YH(M2)-H(M2)*YH(M3))/(H(M3)-H(M2))
- 1750 GOTO 6000
- 1760 '
- 1800 'line between sides m2-m3 and m3-m1 (case 8)
- 1810 X1=(H(M3)*XH(M2)-H(M2)*XH(M3))/(H(M3)-H(M2))
- 1820 Y1=(H(M3)*YH(M2)-H(M2)*YH(M3))/(H(M3)-H(M2))
- 1830 X2=(H(M1)*XH(M3)-H(M3)*XH(M1))/(H(M1)-H(M3))
- 1840 Y2=(H(M1)*YH(M3)-H(M3)*YH(M1))/(H(M1)-H(M3))
- 1850 GOTO 6000
- 1860 '
- 1900 'line between sides m3-m1 and m1-m2 (case 9)
- 1910 X1=(H(M1)*XH(M3)-H(M3)*XH(M1))/(H(M1)-H(M3))
- 1920 Y1=(H(M1)*YH(M3)-H(M3)*YH(M1))/(H(M1)-H(M3))
- 1930 X2=(H(M2)*XH(M1)-H(M1)*XH(M2))/(H(M2)-H(M1))
- 1940 Y2=(H(M2)*YH(M1)-H(M1)*YH(M2))/(H(M2)-H(M1))
- 1950 '
- 2000 '
-
- 6000 GOSUB 20000 'DrawIt -- vecout(x1,y1,x2,y2,z(k))
-
- 7000 NEXT M 'case 0
-
- 8000 'None in triangle
- 8001 NEXT K
-
- 9000 'None in box
- 9001 NEXT I: NEXT J
- 9002 '
-
- 10000 RETURN
-
- 10999 '*****************************************************************
- 11000 'Subroutine
- 11010 H(M)=D(I+IM(M-1),J+JM(M-1))-Z(K)
- 11020 XH(M)=X(I+IM(M-1))
- 11030 YH(M)=Y(J+JM(M-1))
- 11040 RETURN
-
- 12000 'Subroutine
- 12010 H(0)=(H(1)+H(2)+H(3)+H(4))/4 'average heights of four corners to get center height
- 12020 XH(0)=(X(I)+X(I+1))/2
- 12030 YH(0)=(Y(J)+Y(J+1))/2
- 12040 RETURN
-
- 20000 'DrawIt subroutine 3D is squashed into 2D by ignoring z(k)-elevation
- 20010 LINE (X1,Y1)-(X2,Y2)
- 20020 RETURN
-
- 30000 '*****************************************************************